package web.controller.address_telefon;

import hibernate.pojo.User;
import hibernate.pojo_address_telefon.Address;
import hibernate.pojo_address_telefon.AddressBook;
import hibernate.util.HibernateUtil;

import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIData;
import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;

import web.controller.LoginController;

public class AddressBookController {

	private static final Log logger = LogFactory.getLog(LoginController.class);
	
	private List<AddressBook> addressBooks;
	private User user;
	private UIData addressBooksTable;
	private AddressBook addressBook;
	private String addressBookName;
	private int addressBookID;
	private String userFirstName;
	private Boolean deleteConfirmationPopup;
	
	private List<Address> addresses;
	
	public AddressBookController() {
		// TODO Auto-generated constructor stub
	}

	public void setAddressBooks(List<AddressBook> addressBooks) {
		this.addressBooks = addressBooks;
	}

	public List<AddressBook> getAddressBooks() {
		
		try {
			
			FacesContext context = FacesContext.getCurrentInstance();
			user = (User)context.getExternalContext().getSessionMap().get("user");
			
			addressBooks = new ArrayList<AddressBook>();
			Session session = HibernateUtil.getSessionFactory().openSession();
			Criteria criteria = session.createCriteria(AddressBook.class);
			criteria.add(Restrictions.eq("user",user));
			addressBooks = criteria.list();
			
		} catch (Exception exception) {
			logger.error("Kullanici listesi database den okunurken hata olustu!",exception);
		}
		
		if(addressBooks != null && addressBooks.size() > 0){
			return addressBooks;
		} else 
			return null;
	}
	
	public String editAddressBook(){
		String pageToReturn = "";
		try {
			setAddressBook(((AddressBook)addressBooksTable.getRowData()));
			setAddressBookName(getAddressBook().getAddressBookName());
			
			logger.info("Initial User Values!");
			logger.info(getUserFirstName());
			logger.info(getAddressBookID());
			logger.info(getAddressBookName());
			//logger.info(get)
			pageToReturn = "showAddressBookDetailPage";
		} catch (Exception exception) {
			logger.error("Secilen kullaniciyi getirirken hata olustu!", exception);
		}
		
		return pageToReturn;
		
	}
	
	public String updateAddressBookInformation(){
		String pageToRedirect = "";
		
		logger.info("Updated AddressBook Values!");
		logger.info(getAddressBookName());
				
		try {
			
//			FacesContext context = FacesContext.getCurrentInstance();
//			user = (User)context.getExternalContext().getSessionMap().get("user");
			addressBook.setAddressBookName(getAddressBookName());
			
			Session session = HibernateUtil.getSessionFactory().openSession();
			Transaction transaction = session.beginTransaction();
			session.update(addressBook);
			transaction.commit();
		
			pageToRedirect = "updateAddressBookInfoSuccess";
		} catch (Exception exception) {
			logger.error("Error Occured During User Info Update :", exception);
		}		
		
		return pageToRedirect;
	}
	
	public String listAddresses(){
		String pageToRedirect = "";
		
		logger.info("Updated AddressBook Values!");
		if (getAddressBooks().get(0) != null) {
			logger.info(getAddressBooks().get(0).getAddressBookName());
			setAddressBook(getAddressBooks().get(0));
		}
		
		try {
			
			Criteria criteria = HibernateUtil.getSessionFactory().openSession().createCriteria(Address.class);
			criteria.add(Restrictions.eq("addressBook",getAddressBook()));

			addresses = criteria.list();
			if( addresses!= null && addresses.size() > 0) {
				FacesContext context = FacesContext.getCurrentInstance();
				String addressBookID = Integer.toString(addresses.get(0).getAddressBookID());
				//String telephoneBookID2 = ""+telephones.get(0).getTelephoneBookID();
				context.getExternalContext().getSessionMap().put("addressBookID", addressBookID);
				pageToRedirect = "listAddressesSuccess";
			}
			else {
				pageToRedirect = "failure";
			}
			
		} catch (Exception exception) {
			logger.error("Error Occured During User Info Update :", exception);
		}		
		
		return pageToRedirect;
	}
	
	public String showDeleteConfirmationPopup() {

		String pageToRedirect = "";
		setAddressBook(((AddressBook)addressBooksTable.getRowData()));
		setDeleteConfirmationPopup(true);
		
		return pageToRedirect;
	}
	
	public void closeDeleteConfirmationPopup() {
		setDeleteConfirmationPopup(false);
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getUserFirstName() {
		return userFirstName;
	}

	public void setUserFirstName(String userFirstName) {
		this.userFirstName = userFirstName;
	}

	public void setDeleteConfirmationPopup(Boolean deleteConfirmationPopup) {
		this.deleteConfirmationPopup = deleteConfirmationPopup;
	}

	public Boolean getDeleteConfirmationPopup() {
		return deleteConfirmationPopup;
	}

	public UIData getAddressBooksTable() {
		return addressBooksTable;
	}

	public void setAddressBooksTable(UIData addressBooksTable) {
		this.addressBooksTable = addressBooksTable;
	}

	public AddressBook getAddressBook() {
		return addressBook;
	}

	public void setAddressBook(AddressBook addressBook) {
		this.addressBook = addressBook;
	}

	public String getAddressBookName() {
		return addressBookName;
	}

	public void setAddressBookName(String addressBookName) {
		this.addressBookName = addressBookName;
	}

	public int getAddressBookID() {
		return addressBookID;
	}

	public void setAddressBookID(int addressBookID) {
		this.addressBookID = addressBookID;
	}

	public List<Address> getAddresses() {
		return addresses;
	}

	public void setAddresses(List<Address> addresses) {
		this.addresses = addresses;
	}
	
}
